/**
 * 本mixin作用: ( 这名字起的不好lol )
 * - 前往通信
 * - 顶部导航栏效果
 */
import {
  CHATTERS_NAMES,
  CURRENT_USER_KEY,
  d_uniShowToast,
  PROFILE_OF,
  uniNavigate,
  uniShowToast
} from '../../util/misc';
import { didntLoggedIn, uniGetStorageSync, uniSetStorageSync } from '../../util/funcs';
import { CHATTER_TO } from '../../util/rongCloud/messaging';
import { chatMixin } from '../../util/reuse/chatMixin';

export const contact = {
  mixins: [chatMixin],
  methods: {
    navBarEffect(e) {
      let scrollTop = e.scrollTop;

      // 导航栏颜色, 导航栏底部border
      if (scrollTop > this.swiperHeight - 150) {
        this.navBarWhiteOpacity = 1;
        this.navBarBorder = true;
      } else if (scrollTop < this.swiperHeight - 150) {
        this.navBarWhiteOpacity = scrollTop / (this.swiperHeight - 150);
        this.navBarBorder = false;
      }

      //  图标颜色
      if (scrollTop > 0) {
        this.uniIconColor = 'black';
      } else {
        this.uniIconColor = 'white';
      }
    },
    phoneCall(phoneNumber) {
      if (phoneNumber === '') {
        d_uniShowToast({
          title: '暂无号码',
        });
      } else {
        uni.makePhoneCall({
          phoneNumber: phoneNumber, //仅为示例
        });
      }
    },
    /**
     * 融云聊天
     */
    toMessaging(person, houseItem) {
      if (didntLoggedIn(false)) {
        uniNavigate('/login/loginPassword');
        return;
      }
      if (!person.rongcloud_id) {
        // console.log(person);
        uniShowToast('获取聊天用户信息失败')
        return;
      }
      if (houseItem) this.set_sHouseItem(houseItem);
      uniSetStorageSync(CHATTER_TO, person);
      uniNavigate('/chatting/chatting');
    },
    // 环信聊天
    toMessage(chatPerson) {
      /* chatPerson 结构
      * company: (...)
        easemob_account_password: (...)
        head_thumb: (...)
        id: (...)
        name: (...)
        phone: (...)
      * */
      if (didntLoggedIn()) return;

      set_CHATTERS_NAMES(chatPerson);
      // 保存用户信息
      uniSetStorageSync(
        PROFILE_OF + chatPerson.easemob_account_password,
        chatPerson
      );

      // 跳转到聊天页面
      uniNavigate('/chat/chat');
    },
  },
  computed: {
    navCircleColor() {
      let number = 0.2;
      let opacity = 1 - this.navBarWhiteOpacity;
      return 'rgba(0,0,0,' + opacity * number + ')';
    },
    navBarColor() {
      return 'rgba(255,255,255,' + this.navBarWhiteOpacity + ')';
    },
  },
  data() {
    return {
      uniIconColor: 'white',
      navBarBorder: false,
      swiperHeight: 300,
      navBarStyle: {},
      navBarWhiteOpacity: 0,
    };
  },
  mounted() { },
};

/* 设置聊天的双方信息以开始聊天, 保存内容: 我的hxID-头像, 他的hxID-头像-名字
 */
export function set_CHATTERS_NAMES(chatPerson) {
  if (!chatPerson.name) {
    chatPerson.name = '匿名';
  }
  let meUser = uniGetStorageSync(CURRENT_USER_KEY);
  uniSetStorageSync(CHATTERS_NAMES, {
    myProfilePhoto: meUser.headimgurl,
    myName: meUser.easemob_account_password,
    yourProfilePhoto: chatPerson.head_thumb,
    your: chatPerson.easemob_account_password,
    chatUserName: chatPerson.name, // 对方的名字
  });
}
